import axios from 'axios'
// import router from "./router";
import { showLoadingToast, closeToast, showFailToast } from 'vant'
import 'vant/es/toast/style'

let service = axios.create({
  // baseURL: import.meta.env.VITE_APP_BASE_URL,
  baseURL: 'https://sf.mahelei.com/admin',
  headers: {
    'Content-Type': 'multipart/form-data'
  },
  timeout: 8000
})

//声明axios请求拦截器
service.interceptors.request.use(
  (config) => {
    //展示loading效果
    showLoadingToast({
      message: 'loading...',
      forbidClick: true,
      duration: 0,
      overlay: true
    })
    //判断token是否存在
    // let token = localStorage.getItem('token')
    // if (token) {
    //   config.headers['token'] = token
    // }
    return config
  },
  (err) => {
    closeToast()
    return Promise.reject(err)
  }
)

//声明axios响应拦截器
service.interceptors.response.use(
  (response) => {
    //隐藏loading效果
    closeToast()
    // if (response.data.code === 999) {
    //   router.replace("/login");
    //   console.log("token过期");
    // }
    return response
  },
  (error) => {
    showFailToast(error || '请求失败')
    // return Promise.reject(error)
  }
)

export default service
